<template>
	<view class="home-page">
		<image
			class="bg-linear-gradient"
			:src="OSSImgUrl(`bg-linear-gradient.png`)"
			mode="aspectFill"
		/>

		<swiper
			class="swiper"
			v-if="swiperData"
			circular
			:indicator-dots="false"
			:autoplay="true"
			:interval="3000"
			:duration="500"
			:next-margin="20"
			:current="current"
			@animationfinish="bannerChange"
			@change="dotChange"
		>
			<swiper-item v-for="(item, index) of swiperData" :key="index">
				<view class="swiper-item">
					<view class="img-wrap">
						<image
							:src="item.img_url"
							@click="() => gotoTeleplayDetail(item)"
						/>
					</view>
					<view class="content-wrap">
						<view class="title">{{ item.video_name }}</view>
						<view class="tag-list">
							<view class="tag">{{ item.type_name }}</view>
						</view>
						<view class="hot">
							<image
								mode="widthFix"
								class="hot-icon"
								:src="OSSImgUrl(`hot-icon.png`)"
							/>
							<view class="txt">{{ item.v_count }}人观看</view>
						</view>
						<view class="desc">
							{{ item.desc }}
						</view>
						<view class="btn" @click="() => gotoTeleplayDetail(item)">
							<image
								mode="widthFix"
								:src="OSSImgUrl('swiper-see-video-btn.png')"
							/>
						</view>
					</view>
				</view>
			</swiper-item>
		</swiper>
		<!-- 自定义指示点 -->
		<view class="indicator-container" v-if="swiperData">
			<view
				v-for="(item, index) of swiperData"
				:key="index"
				class="indicator"
				:class="{ active: dotCurrent === index }"
			></view>
		</view>

		<view class="home-content">
			<view class="home-title">
				<view class="title-content">
					必看榜
					<image :src="OSSImgUrl('home-title-bg.png')" />
				</view>
				<image
					class="more"
					mode="widthFix"
					:src="OSSImgUrl('qfhy-jxhj-more.png')"
				/>
			</view>

			<view class="bk-warpper">
				<view
					class="bk-item"
					v-for="(item, index) of bkData"
					:key="index"
					@click="() => gotoTeleplayDetail(item)"
				>
					<view class="img-wrap">
						<image class="cover" :src="item.img_url" />
						<image
							class="tip"
							mode="widthFix"
							v-if="index % 2 == 0"
							:src="OSSImgUrl(`bk-tip${index}.png`)"
						/>
					</view>
					<view class="content">
						<view class="title">{{ item.video_name }}</view>
						<view class="count"
							>{{
								`${item.is_status === 0 ? '更新中 · ' : '已完结 · '}` +
								item.menu_count
							}}集</view
						>
					</view>
				</view>
			</view>

			<view class="home-title">
				<view class="title-content">
					新剧推荐
					<image :src="OSSImgUrl('home-title-bg.png')" />
				</view>
				<image
					class="more"
					mode="widthFix"
					:src="OSSImgUrl('qfhy-xjtj-more.png')"
				/>
			</view>

			<view class="xj-wrapper">
				<view
					class="xj-item"
					v-for="(item, index) of xjData"
					:key="index"
					@click="() => gotoTeleplayDetail(item)"
				>
					<image class="cover" :src="item.img_url" />
					<view class="content">
						<view class="title">{{ item.video_name }}</view>
						<view class="count" v-if="item.v_count">
							<image mode="widthFix" :src="OSSImgUrl(`play-icon.png`)" />
							<view class="txt">{{ ToNumber(item.v_count) }}万播放量</view>
						</view>
					</view>
				</view>
			</view>
		</view>

		<FloatingWindow
			v-if="showRecentVideo"
			:IsHide="IsHideFloatWindow"
			:VideoInfo="recentVideo"
			@click.native="gotoPlayVideo"
		>
		</FloatingWindow>
	</view>
</template>

<script>
import {
	getBannerRecommend1,
	getBannerRecommend2,
	getBannerRecommend3,
	getBannerRecommend4,
} from '@/api/teleplay'
import { debounce, OSSImgUrl } from '@/utils'
import FloatingWindow from '@/components/FloatingWindow/index.vue'
export default {
	components: { FloatingWindow },
	data() {
		return {
			current: 0, //轮播图下标
			dotCurrent: 0, // dot下标
			swiperList: [], //轮播图列表

			showRecentVideo: false,
			recentVideo: {}, //历史记录

			IsHideFloatWindow: false,
			swiperData: null,
			bkData: [],
			xjData: [],
			adData: [],
		}
	},
	async onLoad() {
		let res = await getBannerRecommend1()
		this.swiperData = res.data
		console.log('🚀 ~ onLoad ~ this.swiperData:', this.swiperData)

		res = await getBannerRecommend2()
		this.bkData = res.data

		res = await getBannerRecommend3()
		this.xjData = res.data

		res = await getBannerRecommend4()
		this.adData = res.data
	},
	onShow() {
		this.recentVideo = uni.getStorageSync('recentVideo')
		console.log('🚀 ~ onShow ~ this.recentVideo:', this.recentVideo)
		this.showRecentVideo =
			Object.keys(this.recentVideo).length > 0 ? true : false
	},
	onHide() {
		// this.showRecentVideo = false
	},
	onPageScroll(e) {
		this.IsHideFloatWindow === false && (this.IsHideFloatWindow = true)

		this.deboundHideWindow()
	},
	methods: {
		OSSImgUrl: OSSImgUrl,
		deboundHideWindow: debounce(function () {
			this.IsHideFloatWindow = false
		}, 1000),

		// 3.跳转详情页
		gotoTeleplayDetail2(item) {
			// 储存剧集信息
			this.$store.commit('teleplay/setEpisodeInfo', item)
			uni.navigateTo({
				url: `/pages_sub1/media-player/index`,
			})
		},
		gotoTeleplayDetail(item) {
			// 储存剧集信息
			this.$store.commit('teleplay/setEpisodeInfo', item)
			console.log('🚀 ~ gotoTeleplayDetail ~ item:', item)
			if (tt.canIUse('PlayletExtension')) {
				tt.navigateTo({
					url: `/pages/playlet/index?seq=1&tt_album_id=${item.album_id}&tt_episode_id=1&is_continue=1&video_id=${item.video_id}`,
				})
			} else {
				uni.navigateTo({
					url: `/pages_sub1/media-player/index`,
				})
			}
		},
		gotoPlayVideo() {
			// 储存剧集信息
			this.$store.commit('teleplay/setEpisodeInfo', this.recentVideo)
			console.log('this.recentVideo', this.recentVideo)
			if (tt.canIUse('PlayletExtension')) {
				tt.navigateTo({
					url: `/pages/playlet/index?seq=1&tt_album_id=${
						this.recentVideo.album_id
					}&tt_episode_id=${this.recentVideo.episode_id || 1}&is_continue=1`,
				})
			} else {
				uni.navigateTo({
					url: `/pages_sub1/media-player/index`,
				})
			}
		},
		// 5.轮播图切换
		bannerChange(e) {
			this.current = e.current
		},
		dotChange(e) {
			this.dotCurrent = e.detail.current
		},
		ToNumber(value) {
			return value ? Math.round(Number(value) / 10000) : 0
		},
	},
}
</script>

<style lang="scss" scoped>
.home-page {
	height: 100vh;
	box-sizing: border-box;
	padding-top: 60rpx;

	.bg-linear-gradient {
		position: fixed;
		left: 0;
		top: 0;
		width: 100%;
		height: 400rpx;
		z-index: -1;
		transform: translateY(-40%);
	}

	.swiper {
		padding: 0px 20rpx;
		box-sizing: border-box;
		height: 330rpx;

		.swiper-item {
			width: 97%;
			height: 330rpx;
			display: flex;
			border-radius: 8px;
			overflow: hidden;

			.img-wrap {
				width: 226rpx;
				border-radius: 8px;
				overflow: hidden;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.content-wrap {
				position: relative;
				flex: 1;
				box-sizing: border-box;
				padding: 10rpx 20rpx 0px 20rpx;
				// background: linear-gradient(to right, #FFF1E7, #FED5B5);
				background: linear-gradient(to right, #fff5ee, #f9e2d1);

				.title {
					width: 290rpx;
					font-size: 34rpx;
					font-weight: 600;
					white-space: nowrap; /* 禁止换行 */
					overflow: hidden; /* 超出部分隐藏 */
					text-overflow: ellipsis; /* 超出部分显示省略号 */
				}

				.tag-list {
					.tag {
						display: inline-block;
						font-size: 22rpx;
						padding: 2px 10px;
						box-sizing: border-box;
						border-radius: 12px;

						background-color: #ffddc5;
						color: #ff9d58;

						&:not(:first-child) {
							margin-left: 8rpx;
						}
					}
				}

				.hot {
					display: flex;
					align-items: center;
					margin-top: 8rpx;

					.hot-icon {
						width: 24rpx;
					}

					.txt {
						font-size: 22rpx;
						margin-left: 6rpx;
						color: #ff9d58;
					}
				}

				.desc {
					display: -webkit-box;
					line-clamp: 3;
					-webkit-line-clamp: 3;
					text-overflow: ellipsis;
					-webkit-box-orient: vertical;
					overflow: hidden;

					color: #b7b7b7;
					font-size: 22rpx;
					margin-top: 8rpx;
				}

				.btn {
					position: absolute;
					bottom: 20rpx;
					height: 50rpx;

					image {
						width: 144rpx;
					}
				}
			}
		}
	}

	/* 自定义指示点容器 */
	.indicator-container {
		display: flex;
		justify-content: center;
		margin-top: 20rpx;

		/* 指示点样式 */
		.indicator {
			width: 16rpx;
			height: 16rpx;
			margin: 0 8rpx;
			border-radius: 50%;
			background-color: #cfcfcf;
			transition: background-color 0.3s ease;
		}

		/* 当前激活的指示点样式 */
		.indicator.active {
			width: 48rpx;
			border-radius: 30px;
			background: linear-gradient(to right, #fff1e7, #fed5b5);
		}
	}

	.home-content {
		padding: 15rpx;
		box-sizing: border-box;

		.home-title {
			position: relative;
			display: flex;
			height: 70px;
			align-items: center;
			box-sizing: border-box;
			padding: 0px 20rpx;

			.title-content {
				position: relative;
				font-size: 18px;
				font-weight: 600;
				padding-left: 10px;
				box-sizing: border-box;
				white-space: nowrap;

				image {
					left: 50%;
					top: 0;
					z-index: -1;
					position: absolute;
					transform: translate(-50%, 50%);
					width: 115%;
					height: 40rpx;
				}
			}

			.more {
				position: absolute;
				top: 50%;
				transform: translate(0px, -50%);
				right: 0;
				width: 220rpx;
				height: 86rpx;
			}
		}

		.bk-warpper {
			width: 100%;
			background-color: #fff5ec;
			border-radius: 20rpx;
			padding: 40rpx 20rpx;
			box-sizing: border-box;

			display: flex;
			flex-wrap: wrap;

			.bk-item {
				width: 49%;
				display: flex;
				margin-top: 30rpx;

				&:not(:nth-child(2n + 1)) {
					margin-left: 2%;
				}

				.img-wrap {
					position: relative;
					width: 150rpx;

					.tip {
						position: absolute;
						top: -3%;
						left: 9%;
						width: 44rpx;
						height: 56rpx;
					}

					.cover {
						width: 150rpx;
						height: 220rpx;
						border-radius: 14rpx;
					}
				}

				.content {
					width: calc(100% - 150rpx);
					box-sizing: border-box;
					padding-left: 15rpx;
					padding-top: 20rpx;

					.title {
						width: 100%;
						height: 80rpx;
						font-size: 30rpx;
						font-weight: 600;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
						overflow: hidden;
						text-overflow: ellipsis;
					}

					.count {
						margin-top: 20rpx;
						font-size: 26rpx;
						color: #b9b9b9;
					}
				}
			}
		}

		.xj-wrapper {
			width: 100%;
			display: flex;
			flex-wrap: wrap;
			box-sizing: border-box;
			padding: 0px 20rpx 60rpx 20rpx;

			.xj-item {
				width: 31%;
				margin-top: 30rpx;

				&:not(:nth-child(3n + 1)) {
					margin-left: 3.5%;
				}

				.cover {
					width: 100%;
					height: 300rpx;
					border-radius: 8px;
				}

				.content {
					width: 100%;

					.title {
						width: 100%;
						font-size: 30rpx;
						font-weight: 600;
						white-space: nowrap; /* 禁止换行 */
						overflow: hidden; /* 超出部分隐藏 */
						text-overflow: ellipsis; /* 超出部分显示省略号 */
					}

					.count {
						margin-top: 8rpx;
						display: flex;
						align-items: center;

						image {
							width: 20rpx;
							margin-right: 6rpx;
						}

						.txt {
							font-size: 24rpx;
							color: #b9b9b9;
						}
					}
				}
			}
		}
	}
}

::v-deep {
	.u-tabs__wrapper__nav__line {
		position: absolute;
		bottom: 24rpx !important;
		left: 4rpx;
		border-radius: 0px !important;
	}
}

::v-deep {
	.u-line {
		border-color: #fff !important;
	}
}
</style>
